//
//  FrontPageBasicViewController.m
//  NiuBanJinGold
//
//  Created by Billy on 15/11/21.
//  Copyright © 2015年 zzjr. All rights reserved.
//

#import "FrontPageBasicViewController.h"
#import "LoginviewController.h"

@interface FrontPageBasicViewController ()<UIGestureRecognizerDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) UIScrollView *view_scroll ;//支持4S/5
@property (nonatomic, copy) NSString *str_dupe;

@end

@implementation FrontPageBasicViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    
    if (self){
        
        // 返回按钮
        UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0,35, 35)];
        
        [backButton addTarget:self action:@selector(backToPreviousPage) forControlEvents:UIControlEventTouchUpInside];
        
        UIBarButtonItem *barButton_back = [[UIBarButtonItem alloc] initWithCustomView:backButton];
        
        UIImageView *imageView=[[UIImageView alloc]initWithImage:[UIImage imageNamed:@"newBack"]];
        
        imageView.frame=CGRectMake(0, 8,14, 20);
        
        [backButton addSubview:imageView];
        
        [self.navigationItem setLeftBarButtonItem:barButton_back];
    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if(iPhone4){
        
        self.view_scroll = [[UIScrollView alloc] initWithFrame:self.view.frame];
        [self.view_scroll setShowsVerticalScrollIndicator:NO];
        [self.view_scroll setContentSize:CGSizeMake(0, SCREEN_HEIGHT)];
        self.view = self.view_scroll;
    }
    
    [self pageSetting]; //设置页面元素
}


#pragma mark - page setting

- (void) pageSetting {
    
    self.navigationController.navigationBar.translucent = NO;
    [self.view setBackgroundColor:[UIColor whiteColor]];
    // 导航栏背景颜色，要适配
    if (SYSTEM_VERSION >= 7.0) {
        
        self.navigationController.navigationBar.barTintColor = [UsefulUtil colorWithHexString:@"#ffffff"];
        self.navigationController.navigationBar.tintColor = [UsefulUtil colorWithHexString:@"#ffffff"];
    } else {
        
        self.navigationController.navigationBar.tintColor = [UsefulUtil colorWithHexString:@"#ffffff"];
    }
    
    [self.navigationController.navigationBar setTitleTextAttributes:
     [NSDictionary dictionaryWithObjectsAndKeys:[UsefulUtil colorWithHexString:@"#000000"],
      NSForegroundColorAttributeName,[UIFont boldSystemFontOfSize:17], NSFontAttributeName, nil]];
    
    if (SYSTEM_VERSION >= 7.0) {
        
        [self setEdgesForExtendedLayout:UIRectEdgeNone];
    }
    
    // 左右滑动手势
    UISwipeGestureRecognizer *swipeLeft = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiperGestureHandle:)];
    
    [swipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
    
    UISwipeGestureRecognizer *swipeRight = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(swiperGestureHandle:)];
    [swipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
    
    if (iPhone4) {
        
        [self.view_scroll addGestureRecognizer:swipeLeft];
        [self.view_scroll addGestureRecognizer:swipeRight];
    } else {
        
        [self.view addGestureRecognizer:swipeLeft];
        [self.view addGestureRecognizer:swipeRight];
    }
}

#pragma mark - custom return back method

- (void) backToPreviousPage {
    
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - getsture method default implementation

- (void) swiperGestureHandle:(UISwipeGestureRecognizer *) swipe {
    
    UITabBarController *tabbar = (UITabBarController *) CURRENT_WINDOW.rootViewController ;
    
    NSUInteger index = tabbar.selectedIndex;
    
    if  (index < 3 && swipe.direction == UISwipeGestureRecognizerDirectionLeft) {
        
        tabbar.selectedIndex += 1;
        
        CATransition* animation = [CATransition animation];
        [animation setDuration:0.2f];
        [animation setType:kCATransitionMoveIn];
        [animation setSubtype:kCATransitionFromRight];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [[tabbar.view layer]addAnimation:animation forKey:@"switchView"];
        
    }else if( index > 0 && swipe.direction == UISwipeGestureRecognizerDirectionRight){
        
        tabbar.selectedIndex -= 1;
        CATransition* animation = [CATransition animation];
        [animation setDuration:0.2f];
        [animation setType:kCATransitionReveal];
        [animation setSubtype:kCATransitionFromLeft];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
        [[tabbar.view layer]addAnimation:animation forKey:@"switchView"];
    }
}

#pragma mark - hide keyboard

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
